#include "../Header Files/cCamera_AL.h"


cCamera_AL::cCamera_AL()
{
	D3DXMatrixIdentity(&m_mProjectionMatrix);
	D3DXMatrixIdentity(&m_mViewMatrix);
}

cCamera_AL::~cCamera_AL()
{
}

void cCamera_AL::BuildPerspective(float _fFieldOfView, float _fAspect, float _fZNear, float _fZFar)
{
	D3DXMatrixPerspectiveFovLH(&this->m_mProjectionMatrix, _fFieldOfView, 
														   _fAspect,
														   _fZNear,
														   _fZFar);
}

D3DXMATRIX cCamera_AL::GetViewMatrix(bool _bTranslate)
{
	D3DXMATRIX _mRotation(m_mViewMatrix), _mTransform;
	_mRotation._41 = _mRotation._42 = _mRotation._43 = 0;
	
	D3DXMatrixTranspose(&_mRotation, &_mRotation);

	D3DXMatrixIdentity(&_mTransform);
	_mTransform._41 = -(m_mViewMatrix._41);
	_mTransform._42 = -(m_mViewMatrix._42);
	_mTransform._43 = -(m_mViewMatrix._43);

	D3DXMatrixMultiply(&_mTransform, &_mTransform, &_mRotation);
	return (_bTranslate ? _mTransform : _mRotation);
}

void cCamera_AL::SetViewXAxis(D3DXVECTOR3 _vPosition)
{
	m_mViewMatrix._11 = _vPosition.x;
	m_mViewMatrix._12 = _vPosition.y;
	m_mViewMatrix._13 = _vPosition.z;
}

void cCamera_AL::SetViewXAxis(float _fX, float _fY, float _fZ)
{
	m_mViewMatrix._11 = _fX;
	m_mViewMatrix._12 = _fY;
	m_mViewMatrix._13 = _fZ;
}

void cCamera_AL::SetViewYAxis(D3DXVECTOR3 _vPosition)
{
	m_mViewMatrix._21 = _vPosition.x;
	m_mViewMatrix._22 = _vPosition.y;
	m_mViewMatrix._23 = _vPosition.z;
}
void cCamera_AL::SetViewYAxis(float _fX, float _fY, float _fZ)
{
	m_mViewMatrix._21 = _fX;
	m_mViewMatrix._22 = _fY;
	m_mViewMatrix._23 = _fZ;
}

void cCamera_AL::SetViewZAxis(D3DXVECTOR3 _vPosition)
{
	m_mViewMatrix._31 = _vPosition.x;
	m_mViewMatrix._32 = _vPosition.y;
	m_mViewMatrix._33 = _vPosition.z;
}
void cCamera_AL::SetViewZAxis(float _fX, float _fY, float _fZ)
{
	m_mViewMatrix._31 = _fX;
	m_mViewMatrix._32 = _fY;
	m_mViewMatrix._33 = _fZ;
}

void cCamera_AL::SetViewPosition(D3DXVECTOR3 _vPosition)
{
	m_mViewMatrix._41 = _vPosition.x;
	m_mViewMatrix._42 = _vPosition.y;
	m_mViewMatrix._43 = _vPosition.z;
}
void cCamera_AL::SetViewPosition(float _fX, float _fY, float _fZ)
{
	m_mViewMatrix._41 = _fX;
	m_mViewMatrix._42 = _fY;
	m_mViewMatrix._43 = _fZ;
}

/*=================================================================
Local Transform Functions
//===============================================================*/
void cCamera_AL::ViewRotateLocalX(float _fAngle)
{
	D3DXMATRIX  _mRotation;
	D3DXVECTOR3 _vLocalX(m_mViewMatrix._11, m_mViewMatrix._12, m_mViewMatrix._13);
	D3DXMatrixRotationAxis(&_mRotation, &_vLocalX, _fAngle);

	D3DXVECTOR4 Position(m_mViewMatrix._41, 
						m_mViewMatrix._42,
						m_mViewMatrix._43, 
						m_mViewMatrix._44);
	m_mViewMatrix._41 = m_mViewMatrix._42 = m_mViewMatrix._43 = 0.0f;

	D3DXMatrixMultiply(&m_mViewMatrix, &m_mViewMatrix, &_mRotation);
	m_mViewMatrix._41 = Position.x; m_mViewMatrix._42 = Position.y;
	m_mViewMatrix._43 = Position.z; m_mViewMatrix._44 = Position.w;
}

void cCamera_AL::ViewRotateLocalY(float _fAngle)
{
	D3DXMATRIX  _mRotation;
	D3DXVECTOR3 _vLocalY(m_mViewMatrix._21, m_mViewMatrix._22, m_mViewMatrix._23);
	D3DXMatrixRotationAxis(&_mRotation, &_vLocalY, _fAngle);

	D3DXVECTOR4 Position(m_mViewMatrix._41, 
						m_mViewMatrix._42,
						m_mViewMatrix._43, 
						m_mViewMatrix._44);

	m_mViewMatrix._41 = m_mViewMatrix._42 = m_mViewMatrix._43 = 0.0f;

	D3DXMatrixMultiply(&m_mViewMatrix, &m_mViewMatrix, &_mRotation);
	m_mViewMatrix._41 = Position.x; m_mViewMatrix._42 = Position.y;
	m_mViewMatrix._43 = Position.z; m_mViewMatrix._44 = Position.w;
}
void cCamera_AL::ViewRotateLocalZ(float _fAngle)
{
	D3DXMATRIX  _mRotation;
	D3DXVECTOR3 _vLocalZ(m_mViewMatrix._31, m_mViewMatrix._32, m_mViewMatrix._33);
	D3DXMatrixRotationAxis(&_mRotation, &_vLocalZ, _fAngle);

	D3DXVECTOR4 Position(m_mViewMatrix._41, 
						m_mViewMatrix._42,
						m_mViewMatrix._43, 
						m_mViewMatrix._44);
	m_mViewMatrix._41 = m_mViewMatrix._42 = m_mViewMatrix._43 = 0.0f;

	D3DXMatrixMultiply(&m_mViewMatrix, &m_mViewMatrix, &_mRotation);
	m_mViewMatrix._41 = Position.x; m_mViewMatrix._42 = Position.y;
	m_mViewMatrix._43 = Position.z; m_mViewMatrix._44 = Position.w;
}
 
void cCamera_AL::ViewTranslateLocal(D3DXVECTOR3 _vAxis, bool _bFPS)
{
	if (_bFPS)
	{
		ViewTranslateLocalX(_vAxis.x, _bFPS);
		ViewTranslateLocalY(_vAxis.y, _bFPS);
		ViewTranslateLocalZ(_vAxis.z, _bFPS);
		return;
	}

	ViewTranslateLocalX(_vAxis.x);
	ViewTranslateLocalY(_vAxis.y);
	ViewTranslateLocalZ(_vAxis.z);
}

void cCamera_AL::ViewTranslateLocalX(float _fScale, bool _bFPS)
{
	if (_bFPS)
	{
		D3DXVECTOR3 _vTranslate((this->m_mViewMatrix._11),
			0.0f,
			(m_mViewMatrix._13));
		D3DXVec3Normalize(&_vTranslate,
			&_vTranslate);

		m_mViewMatrix._41 += _vTranslate.x * _fScale;
		m_mViewMatrix._42 += _vTranslate.y * _fScale;
		m_mViewMatrix._43 += _vTranslate.z * _fScale;

		return;
	}
	m_mViewMatrix._41 += (m_mViewMatrix._11 * _fScale);
	m_mViewMatrix._42 += (m_mViewMatrix._12 * _fScale);
	m_mViewMatrix._43 += (m_mViewMatrix._13 * _fScale);
}

void cCamera_AL::ViewTranslateLocalY(float _fScale, bool _bFPS)
{
	if (_bFPS)
	{
		D3DXVECTOR3 _vTranslate((m_mViewMatrix._21),
			0.0f,
			(m_mViewMatrix._23));
		D3DXVec3Normalize(&_vTranslate,
			&_vTranslate);

		m_mViewMatrix._41 += _vTranslate.x * _fScale;
		m_mViewMatrix._42 += _vTranslate.y * _fScale;
		m_mViewMatrix._43 += _vTranslate.z * _fScale;

		return;
	}
	m_mViewMatrix._41 += (m_mViewMatrix._21 * _fScale);
	m_mViewMatrix._42 += (m_mViewMatrix._22 * _fScale);
	m_mViewMatrix._43 += (m_mViewMatrix._23 * _fScale);
}

void cCamera_AL::ViewTranslateLocalZ(float _fScale, bool _bFPS)
{
	if (_bFPS)
	{
		D3DXVECTOR3 _vTranslate((m_mViewMatrix._31),
			0.0f,
			(m_mViewMatrix._33));
		D3DXVec3Normalize(&_vTranslate,
			&_vTranslate);

		m_mViewMatrix._41 += _vTranslate.x * _fScale;
		m_mViewMatrix._42 += _vTranslate.y * _fScale;
		m_mViewMatrix._43 += _vTranslate.z * _fScale;

		return;
	}
	m_mViewMatrix._41 += (m_mViewMatrix._31 * _fScale);
	m_mViewMatrix._42 += (m_mViewMatrix._32 * _fScale);
	m_mViewMatrix._43 += (m_mViewMatrix._33 * _fScale);
}

void cCamera_AL::ViewRotateGlobalX(float _fAngle)
{
	D3DXMATRIX _mRotation;
	D3DXVECTOR4 Position(m_mViewMatrix._41, 
						m_mViewMatrix._42,
						m_mViewMatrix._43, 
						m_mViewMatrix._44);
	D3DXMatrixRotationX(&_mRotation, _fAngle);

	m_mViewMatrix._41 = m_mViewMatrix._42 = m_mViewMatrix._43 = 0.0f;

	D3DXMatrixMultiply(&m_mViewMatrix, &m_mViewMatrix, &_mRotation);
	m_mViewMatrix._41 = Position.x; m_mViewMatrix._42 = Position.y;
	m_mViewMatrix._43 = Position.z; m_mViewMatrix._44 = Position.w;
}

void cCamera_AL::ViewRotateGlobalY(float _fAngle)
{
	D3DXMATRIX _mRotation;
	D3DXVECTOR4 Position(m_mViewMatrix._41, 
						m_mViewMatrix._42,
						m_mViewMatrix._43, 
						m_mViewMatrix._44);
	D3DXMatrixRotationY(&_mRotation, _fAngle);

	m_mViewMatrix._41 = m_mViewMatrix._42 = m_mViewMatrix._43 = 0.0f;

	D3DXMatrixMultiply(&m_mViewMatrix, &m_mViewMatrix, &_mRotation);
	m_mViewMatrix._41 = Position.x; m_mViewMatrix._42 = Position.y;
	m_mViewMatrix._43 = Position.z; m_mViewMatrix._44 = Position.w;
}

void cCamera_AL::ViewRotateGlobalZ(float _fAngle)
{
	D3DXMATRIX _mRotation;
	D3DXVECTOR4 Position(m_mViewMatrix._41, 
						m_mViewMatrix._42,
						m_mViewMatrix._43, 
						m_mViewMatrix._44);
	D3DXMatrixRotationZ(&_mRotation, _fAngle);

	m_mViewMatrix._41 = m_mViewMatrix._42 = m_mViewMatrix._43 = 0.0f;

	D3DXMatrixMultiply(&m_mViewMatrix, &m_mViewMatrix, &_mRotation);
	m_mViewMatrix._41 = Position.x; m_mViewMatrix._42 = Position.y;
	m_mViewMatrix._43 = Position.z; m_mViewMatrix._44 = Position.w;
}